﻿using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace JQAutoUpdate
{
    public class ToolKits
    {
        /// <summary>
        /// 如果指定文件夹不存在，则创建
        /// </summary>
        /// <param name="path"></param>
        public static void PrepareDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 解压zip文件到指定文件夹下
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="TargetDir"></param>
        /// <returns></returns>
        public static bool Unzip(string sourceFile,string TargetDir,EventHandler<ExtractProgressEventArgs> unzipProgress = null)
        {
            using (ZipFile zipfile = new ZipFile(sourceFile))
            {
                if (unzipProgress != null)
                {
                    zipfile.ExtractProgress += unzipProgress; 
                }
                zipfile.ExtractAll(TargetDir,ExtractExistingFileAction.OverwriteSilently);
            }
            return true;
        }


        
        /// <summary>
        /// windows服务启停
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="isOpen"></param>
        public static void ServiceController(bool isOpen)
        {
            Process process = new Process();
            string pathStop = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts", "StopService.bat");
            string pathStart = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts", "StartService.bat");
            if (isOpen)
            {
                process.StartInfo.FileName = pathStart;
            }
            else
            {
                process.StartInfo.FileName = pathStop;

            }
            process.Start();
        }

        /// <summary>
        /// IIS启停
        /// </summary>
        /// <param name="isOpen"></param>
        public static void IISController(bool isOpen)
        {
            #region Prepare Batch Files
            string batchStart = @"@echo off
                                  net start iisadmin
                                  net start w3svc
                                  net start msftpsvc ";
            string batchClose = @"@echo off
                                  net stop w3svc
                                  net stop iisadmin
                                  net stop msftpsvc ";
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
            ToolKits.PrepareDirectory(path);
            string filePath_start = $"{path}\\StartIIS.bat";
            string filePath_Stop = $"{path}\\StopIIS.bat";
            if (!File.Exists(filePath_start))
            {
                File.WriteAllText(filePath_start, batchStart);
            }
            if (!File.Exists(filePath_Stop))
            {
                File.WriteAllText(filePath_Stop,batchClose);
            }
            #endregion

            Process process = new Process();
            if (isOpen)
            {
                process.StartInfo.FileName = filePath_start;
            }
            else
            {
                process.StartInfo.FileName = filePath_Stop;
            }
            process.Start();
        }
    }
}
